/* Copyright (c) 2011 by Mani Brundha Kattamuri, Haritha Subramanyam
 *
 * Project Info:  http://code.google.com/p/andriod-travelapp-csc780/wiki/Design
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */
package com.example.TravelApp.search.advanced;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;
import com.example.TravelApp.R;
import com.example.TravelApp.constants.MapConstants;
import com.example.TravelApp.search.common.PlaceObject;
import com.example.TravelApp.search.common.helper.PlacesDirectionHelper;
import com.example.TravelApp.search.common.helper.JSONParseHelper;

import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.app.ListActivity;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * This activity is called by the Advanced activity when the user clicks on
 * "Go Find" button. The layout is described in file
 * <code>res/layout/option_layout.xml</code>. This activity displays the two
 * trip plans to the user 1. Trip Plan 1 (By Rating) - This trip plan is
 * constructed such that the user gets to see the top rated places 2. Trip Plan
 * 2 (By Max Places) This trip plan is constructed such that the user covers
 * places the maximum number of places in given time
 * 
 * @author Haritha Subramanyam, Mani Brundha
 * 
 */
public class AdvancedOptionOverviewActivity extends ListActivity {
	String place, intrest, time;
	private ArrayList<PlaceObject> list;
	private ArrayList<PlaceObject> listByRating = new ArrayList<PlaceObject>();
	private ArrayList<PlaceObject> listByTime = new ArrayList<PlaceObject>();
	String[] Options = { "", "", "" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Users choices are received via content from Advanced activity
		place = (String) getIntent().getCharSequenceExtra("Place");
		time = (String) getIntent().getCharSequenceExtra("Time");
		intrest = (String) getIntent().getCharSequenceExtra("Intrest");
		// User preferences are sent and JSON object is received
		JSONObject json = JSONParseHelper.getLocationInfo(place,
				MapConstants.GOOGLE_PLACES_RADIUS_VALUE, intrest,
				MapConstants.GOOGLE_PLACES_NAME_VALUE,
				MapConstants.GOOGLE_PLACES_SENSOR_VALUE);
		// List of PlaceObjects is constructed from JSON Object
		this.list = JSONParseHelper.getList(json);

		// TO prevent Null Pointers; proceeds only if the list is not empty
		if (this.list.isEmpty() != true) {
			setContentView(R.layout.option_layout);

			// The two options to be given to the user are constructed
			listByRating = PlacesDirectionHelper.optionByRating(this.list,
					time, place);
			listByTime = PlacesDirectionHelper.optionByTime(this.list, time,
					place);
			updateOverviewArray(this.listByTime, this.listByRating);
			setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
					this.Options));
			ListView listView = getListView();
			listView.setOnItemClickListener(new OnItemClickListener() {
				/**
				 * Implements an action listener for collecting the option
				 * selected by the user. Listens to the user's click and starts
				 * the AdvancedTabOptionDetailsActivity which shows the details
				 * of the selected option if first option is selected Trip Plan
				 * 1 (By Rating) is displayed if second option is selected Trip
				 * Plan 2 (By Max Places) is displayed
				 */
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					/*
					 * Construct an Intent to launch OptionDetailsActivity.
					 */
					Intent intent = new Intent(
							AdvancedOptionOverviewActivity.this,
							AdvancedTabOptionDetailsActivity.class);
					if (position == 1) {
						intent.putParcelableArrayListExtra("placesList",
								listByTime).putExtra("Place", place);

					}
					if (position == 0) {
						intent.putParcelableArrayListExtra("placesList",
								listByRating).putExtra("Place", place);

					}
					startActivityForResult(intent, 0);
				}

			});
		} else {
			// If no is data available displays proper message on the screen
			setContentView(R.layout.no_results);
		}

	}

	/**
	 * An overview of the places in the lists are constructed
	 * 
	 * @param timeList
	 *            - List constructed such that the user gets to see the most
	 *            number of places
	 * @param ratingList
	 *            - List constructed such that the user gets to see the top
	 *            rated places
	 */
	private void updateOverviewArray(List<PlaceObject> timeList,
			List<PlaceObject> ratingList) {
		Options[0] = "Trip Plan 1 (By Rating): \n";
		Options[1] = "Trip Plan 2 (By Max Places): \n";

		for (int i = 0; i < timeList.size(); i++) {
			Options[1] = Options[1] + "Stop:" + (i + 1) + "  "
					+ timeList.get(i).getVenueName() + "\n";
		}
		for (int i = 0; i < ratingList.size(); i++) {

			Options[0] = Options[0] + "Stop:" + (i + 1) + "  "
					+ ratingList.get(i).getVenueName() + "\n";
		}

	}

	/**
	 * Called whenever AdvancedTabOptionDetailsActivity terminates.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK) {
			Intent intent = getIntent();
			finish();
			startActivity(intent);

		}
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent();
		setResult(RESULT_OK, intent);
		super.onBackPressed();
	}

}
